package com.zysf.library_boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zysf.library_boot.dto.MyCommentDto;
import com.zysf.library_boot.dto.MyReaderCommentDto;
import com.zysf.library_boot.dto.PostsDto;
import com.zysf.library_boot.dto.ReaderCommentDto;
import com.zysf.library_boot.entity.LendList;
import com.zysf.library_boot.entity.Posts;
import com.zysf.library_boot.entity.ReaderComment;
import com.zysf.library_boot.entity.ReaderInfo;
import com.zysf.library_boot.service.ReaderCommentService;
import com.zysf.library_boot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/readerComment")
public class ReaderCommentController {

    @Autowired
    ReaderCommentService readerCommentService;

    @RequestMapping("/getCustomReaderComment")
    public Result getCustomReaderComment(@RequestParam int currentPage, @RequestParam int pageSize) {
        Page<ReaderCommentDto> page = new Page<>(currentPage, pageSize);
        IPage<ReaderCommentDto> readerCommentDtoIPage = readerCommentService.getCustomReaderComment(page);

        Map<String, Object> result = new HashMap<>();
        result.put("records", readerCommentDtoIPage.getRecords());
        result.put("total", readerCommentDtoIPage.getTotal());

        return Result.ok(result);
    }

    @PostMapping("/addReaderComment")
    public Result addReaderComment(@RequestBody ReaderComment readerComment) {
        boolean saved = readerCommentService.save(readerComment);
        if (saved) {
            return Result.ok("添加成功");
        } else {
            return Result.fail("添加失败");
        }
    }

    @RequestMapping("/showPosts")
    public Result showPosts(@RequestParam Integer id, @RequestParam int currentPage, @RequestParam int pageSize) {
        Page<MyReaderCommentDto> page = new Page<>(currentPage, pageSize);
        IPage<MyReaderCommentDto> myReaderCommentDtoIPage = readerCommentService.getCustomMyReaderComment(id, page);

        Map<String, Object> result = new HashMap<>();
        result.put("records", myReaderCommentDtoIPage.getRecords());
        result.put("total", myReaderCommentDtoIPage.getTotal());

        return Result.ok(result);
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        boolean q = readerCommentService.removeById(id);
        return Result.ok(q);
    }
}
